package com.workerhelping.utils;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5Util {
    public static String encode(String sourcePassword) {
        // 获取MessageDigest对象
        String algorithm = "md5";
        MessageDigest messageDigest = null;
        try {
            messageDigest = MessageDigest.getInstance(algorithm);
            // 获取明文密码对应的字符串数组
            byte[] source_passwordBytes = sourcePassword.getBytes();
            // 执行加密
            byte[] output = messageDigest.digest(source_passwordBytes);
            // 创建BigInteger对象
            int signum = 1;
            BigInteger bigInteger = new BigInteger(signum, output);
            // 按照16进制将bigInteger转换为字符串
            int radix = 16;
            return bigInteger.toString(radix);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return null;
        }
    }
}
